package controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.javaws.HtmlOptions;
import model.OrderDao;
import model.Shop;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/orderShop")
public class OrderShopServlet extends HttpServlet {

    //使用这个数据类型来将数据转成json格式
    private ObjectMapper objectMapper = new ObjectMapper();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("utf8");
        resp.setCharacterEncoding("utf8");
        resp.setContentType("text/html;charset=utf-8");

        //获取要查询的订单ID
        int orderId = Integer.parseInt(req.getParameter("orderId"));
        //获取orderdetail表中所以订单编号为orderID的订单，并用LIst存储返回服务器
        List<Shop> shops = new ArrayList<>();
        OrderDao orderDao = new OrderDao();
        shops = orderDao.selectAllOrderShop(orderId);

        //将shops转为json发送回去
        String respJson = objectMapper.writeValueAsString(shops);
        System.out.println(respJson);
        resp.getWriter().write(respJson);







    }
}
